/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of qtweather-qml
**
** qtweather-qml is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** qtweather-qml is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0
import com.nokia.extras 1.0

Page {
    id: mainPage
    tools: commonTools

	signal finished(string zipcode)
	property bool busy: weatherModel.status === XmlListModel.Ready ? false : true
	property string zipcode
	property string title
	property string hazards

	//present a warning for hazards
	function warn(warningText){
		var warning = banner.createObject(mainPage)
		warning.text = warningText
		warning.show()
		warning.destroy(20000)
	}
	Timer{
		id: bannerTimer
		property string warning
		onWarningChanged: start()
		interval: 1000
		onTriggered: warn(warning)
	}

	Component{
		id: banner
		InfoBanner{
			id: info
			text: weatherModel.get(0).hazards
			timerEnabled: true
			iconSource: "image://theme/icon-m-conversation-contact-attention-inverse"
			timerShowTime: 10000
			Component.onCompleted: {show(); info.destroy(timerShowTime*2)}
		}
	}

	onZipcodeChanged: weatherModel.source = "http://umcs.maine.edu/~naddeoa/qtweather-qml?zipcode=" + zipcode

	Timer{//update the app every 4 hours
		id: timer
		running: zipcode ?  true : false
		interval: 1440000
		repeat: true
		triggeredOnStart: false
		onTriggered: weatherModel.source = "http://umcs.maine.edu/~naddeoa/qtweather-qml?zipcode=" + zipcode
	}

	WeatherModel{
		id: weatherModel
		onStatusChanged: {
			if(status === XmlListModel.Ready && get(0)){
				mainPage.title = get(0).city
//				mainPage.hazards = get(0).hazards
				if(get(0).hazards)
					bannerTimer.warning = get(0).hazards
				Utilities.setHome(zipcode,get(0).city)
				mainPage.finished(zipcode)
			}

		}
	}



	ListView{
		id: list
		orientation: ListView.Horizontal
		anchors.fill: parent
		snapMode: ListView.SnapToItem
		flickDeceleration: 10000
		model:weatherModel
		cacheBuffer: 1600
		highlightRangeMode: ListView.StrictlyEnforceRange
		preferredHighlightBegin: 0
		preferredHighlightEnd:0

		delegate: DayDelegate{
			height: list.height
			width: list.width
			shown: index === list.currentIndex ? true : false
		}
	}
}
